package com.hd.crdao.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.MyBankCardAdapter;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.BankDataBean;
import com.hd.crdao.bean.MyBankCardBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/5/23 0023
 */
public class MyBankCardActivity extends BaseSecondActivity {
    @BindView(R.id.mybankcard)
    ExpandHeadView mMyBankCard;
    @BindView(R.id.bankcardlist)
    ListView mBankcardlist;
    @BindView(R.id.hasDataLayout)
    FrameLayout mHasDataLayout;
    public String[] mBanklists;
    private List<String> banklists = new ArrayList<>();
    public List<MyBankCardBean.RowsBean> mMyBankCardBeanRows;
    public boolean mIsWannaData;

    @Override
    protected int setContentView() {
        return R.layout.activity_mybankcard;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mMyBankCard;
    }

    @Override
    protected void initView() {
        mIsWannaData = getIntent().getBooleanExtra("isWannaData", false);
        ImageView rightPlus = mMyBankCard.getHeadViewRightPlus();
        rightPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(UIUtils.getContext(), AddBankCardActivity.class));
                finish();
            }
        });
        mBanklists = getResources().getStringArray(R.array.banklist);
        for (int i = 0; i < mBanklists.length; i++) {
            banklists.add(i, mBanklists[i]);
        }
        CallNetwork();
        initListener();

    }

    /**
     * 设置监听
     */
    private void initListener() {
        mBankcardlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MyBankCardBean.RowsBean rowsBean = mMyBankCardBeanRows.get(position);
                if (mIsWannaData) {
                    BankDataBean bankDataBean = new BankDataBean();
                    bankDataBean.setBankName(rowsBean.getBankCode().getBcNote());
                    bankDataBean.setAccountName(rowsBean.getBcName());
                    bankDataBean.setBankNumber(rowsBean.getBcCardnum());
                    bankDataBean.setId(rowsBean.getId());
                    EventBus.getDefault().post(bankDataBean);
                    finish();
                } else {
                    Intent intent = new Intent(UIUtils.getContext(), AddBankCardActivity.class);
                    intent.putExtra("bankid", rowsBean.getId());
                    intent.putExtra("flag", true);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    /**
     * 请求网络
     */
    private void CallNetwork() {
        String businessid = SPUtils.getString(Config.businessId, "");
        OkHttpUtils.post().url(Constants.BANKCARDLIST_URL)
                //   .addParams("pageNo", "1")
                //  .addParams("pageSize", "")
                .addParams("data", businessid + "~")
                .addParams("sign", Constants.SIGN).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("服务器连接失败或异常");
            }

            @Override
            public void onResponse(String response, int id) {
                ALog.d(response);
                Gson gson = new Gson();
                MyBankCardBean myBankCardBean = gson.fromJson(response, MyBankCardBean.class);
                mMyBankCardBeanRows = myBankCardBean.getRows();
                mBankcardlist.setAdapter(new MyBankCardAdapter(mMyBankCardBeanRows, banklists));
                if (mMyBankCardBeanRows.isEmpty()) {
                    mHasDataLayout.setVisibility(View.VISIBLE);
                } else {
                    mHasDataLayout.setVisibility(View.GONE);
                }
            }
        });
    }


}
